package com.eqdd.yiqidian.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.view.NestedScrollingParent;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.eqdd.library.percentUtil.WindowUtil;

/**
 * Created by lvzhihao on 17-6-3.
 */

public class NestedLayout extends LinearLayout implements NestedScrollingParent {


    private int top=300* WindowUtil.width/640;
    public NestedLayout(Context context) {
        super(context);
    }

    public NestedLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        System.out.println("当前类=NestedLayout.NestedLayout()");
    }

    @Override
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
        System.out.println(nestedScrollAxes + "nestedScrollAxes");
        if ((nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0) {
            System.out.println("true");
            return true;
        }
        return false;
    }

    @Override
    public void onNestedScrollAccepted(View child, View target, int nestedScrollAxes) {

        System.out.println("当前类=NestedLayout.onNestedScrollAccepted()");
    }

    @Override
    public void onStopNestedScroll(View target) {
        System.out.println("当前类=NestedLayout.onStopNestedScroll()");
    }

    @Override
    public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        System.out.println("当前类=NestedLayout.onNestedScroll()");
    }

    @Override
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
        System.out.println("当前类=NestedLayout.onNestedPreScroll()");
        System.out.println(getScrollY()+"getScrollY()");
        boolean isConsume=dy > 0&&getScrollY()<top;
        boolean isConsume1=dy < 0 && getScrollY() > 0&&!ViewCompat.canScrollVertically(target, -1);
        if (isConsume||isConsume1) {
            scrollBy(0,dy);
            consumed[1] = dy;
        }
    }

    @Override
    public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
        System.out.println("当前类=NestedLayout.onNestedFling()");
        return false;
    }

    @Override
    public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
        System.out.println("当前类=NestedLayout.onNestedPreFling()");
        return true;
    }

    @Override
    public int getNestedScrollAxes() {
        System.out.println("当前类=NestedLayout.getNestedScrollAxes()");
        return 0;
    }
}
